﻿/*
 * Project	    : Outlook 2003 Style Navigation Pane
 *
 * Author       : Muhammed ŞAHİN
 * eMail        : muhammed.sahin@gmail.com
 *
 * Description  : NavigateBar collapsible screen
 * 
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WP.Controls.OutlookStyleNavigateBar
{
    /// <summary>
    /// If navigatebar width equal or small minimum size then show related control in this form
    /// </summary>
    class NavigateBarCollapsibleScreen : Form
    {

        const int DISTANCE = 5;

        # region Shadow Form

        private const int CS_DROPSHADOW = 0x00020000;
        protected override CreateParams CreateParams
        {
            [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.LinkDemand, UnmanagedCode = true)]
            get
            {
                CreateParams parameters = base.CreateParams;

                // For Shadow

                if (NavigateBarHelper.IsXPOrAbove)
                    parameters.ClassStyle = (parameters.ClassStyle | CS_DROPSHADOW);

                return parameters;
            }
        }
        #endregion

        #region  CaptionBand
        NavigateBarCaption caption;
        public NavigateBarCaption CaptionBand
        {
            get { return caption; }
        }
        #endregion

        #region IsShowWindow
        bool isShowWindow = false;
        public bool IsShowWindow
        {
            get { return isShowWindow; }
            set { isShowWindow = value; }
        }
        #endregion

        NavigateBar navigateBar = null;
        Panel panelControl = new Panel();

        #region Yapıcı Method
        public NavigateBarCollapsibleScreen(NavigateBar tNavigateBar)
        {
            navigateBar = tNavigateBar;
            // Get positon on desktop screen
            Point p = new Point(navigateBar.Location.X + Width, navigateBar.Location.Y);
            DesktopLocation = this.PointToScreen(p);

            // Caption info
            caption = new NavigateBarCaption(navigateBar);
            caption.Height = 30;
            caption.CollapseButton.Visible = false;

            //
            //panelControl.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;

            this.Resize += new EventHandler(NavigateBarCollapsibleScreen_Resize);
            this.FormClosing += new FormClosingEventHandler(NavigateBarCollapsibleScreen_FormClosing);

            Controls.Add(caption);
            Controls.Add(panelControl);

            //
            SetStyle(ControlStyles.ResizeRedraw, true);
        }

        /// <summary>
        /// Kullanıcı kapatamamalı
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void NavigateBarCollapsibleScreen_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason != CloseReason.ApplicationExitCall ||
                e.CloseReason != CloseReason.TaskManagerClosing ||
                e.CloseReason != CloseReason.WindowsShutDown)
                e.Cancel = true;
        }

        /// <summary>
        /// if resized chnage panel width
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void NavigateBarCollapsibleScreen_Resize(object sender, EventArgs e)
        {
            panelControl.Anchor = AnchorStyles.None;

            panelControl.Left = DISTANCE;
            panelControl.Width = this.Width - DISTANCE * 2;

            if (navigateBar.SelectedButton != null && navigateBar.SelectedButton.IsShowCollapseScreenCaption)
            {
                panelControl.Top = caption.Height + DISTANCE;
                panelControl.Height = this.Height - caption.Height - DISTANCE * 2;
            }
            else
            {
                panelControl.Top = DISTANCE;
                panelControl.Height = this.Height - DISTANCE * 2;
            }

            panelControl.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;

        }
        #endregion

        #region SetControl
        /// <summary>
        /// Set showed control in collapsed screen
        /// </summary>
        /// <param name="tControl"></param>
        public void SetControl(Control tControl)
        {
            if (tControl == null)
                return;

            tControl.Dock = DockStyle.Fill;
            panelControl.Controls.Clear();
            panelControl.Controls.Add(tControl);
            tControl.Focus();

        }

        #endregion

        #region overrided Methods
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);
            NavigateBarHelper.PaintGradientControl(this,
                navigateBar.NavigateBarColorTable.ButtonNormalBegin,
                navigateBar.NavigateBarColorTable.ButtonNormalEnd);

            Rectangle r = this.ClientRectangle;
            r.Width--;
            r.Height--;
            e.Graphics.DrawRectangle(new Pen(navigateBar.NavigateBarColorTable.BorderColor), r);
        }
        #endregion


    }
}